# -*- coding: utf-8 -*-
class BrandController < ApplicationController
  before_filter :employee_authorize
  before_filter :sys_before_log
  before_filter :employee_action_hx
  before_filter :privilege_filter,:except=>[:newBrand,:editBrand,:brandLock]

  def brandList

  end
  
  def create
    begin
      brand = Brand.new
      brand.company_id =  session[:company_id]
      brand.department_id =  session[:department_id]
      brand.employee_id =  session[:employee_id]
      brand.name = params[:name]
      brand.vendor_id = params[:vendor_id]
      brand.description = params[:description]
      brand.save!
      info = "成功"
    rescue Exception => e
      info = e.to_s
      logger.info info
      DbLog.info(session[:company_id], session[:department_id], session[:employee_id], params[:controller], params[:action], request.remote_ip ,e.to_s)
    end
    
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

  def update
    begin
      brand = Brand.find(params[:id])
      brand.employee_id =  session[:employee_id]
      brand.name = params[:name]
      brand.vendor_id = params[:vendor_id]
      brand.description = params[:description]
      brand.save!
      info = "成功"
    rescue Exception => e
      info = e.to_s
      logger.info info
      DbLog.info(session[:company_id], session[:department_id], session[:employee_id], params[:controller], params[:action], request.remote_ip ,e.to_s)
    end

    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

  def getAllBrands
    forCBB = params[:for_cbb]
    if forCBB == 'true'
      brands = Brand.find(:all,:conditions=>["is_locked = ?",0])
    else
      brands = Brand.find(:all)
    end
    brands.each do |brand|
      logger.info brand.name
    end
    jsonStr="{totalProperty:"+brands.length.to_s+",root:"+brands.to_json(:include=>[:vendor])+"}"
    logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end

  def getBrandById
    brand = Brand.find(params[:id])
    rootStr = brand.to_json()
    jsonStr = "{root:["+rootStr+"]}"
    logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end

  #锁定或解锁销售类型
  def brandLock
    brand = Brand.find(params[:id])
    if brand.is_locked == 1
      brand.is_locked = 0
    else
      brand.is_locked = 1
    end
    brand.save!
    info = "成功"
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end
  
end
